<?php

class Test extends MY_Controller {
	
	function __construct()
	{
		parent::MY_Controller();
		
		$this->load->helper('file');
		$this->load->helper('url');
		$this->load->helper('date');
		$this->load->library('parser');
		$this->load->library('excel');
		$this->load->library('table');		
		
		$this->template->enable_parser(TRUE);
		
		// data head
		$this->data["head"]['heading'] = "NAVIGATION";
		$this->data["head"]['menu_links'] = array(
			array('name'=>'Trang chu import', 'url'=>'/'),
			array('name'=>'Quan ly hoa don mua hang', 'url'=>'bill'),
			array('name'=>'Quan ly hang hoa', 'url'=>'product')
		);
	}
	
	function index()
	{
		// get folder content
		$files = get_dir_file_info( FILEPATH );
		
		foreach($files as $file_name => $file_info) {
			$a_actions['edit'] = anchor('test/import/'. $file_name, 'import', 'title="Import this file"');
			$a_actions['preview'] = anchor('test/preview/'. $file_name, 'preview', 'title="Preview file"');
			$mime_type = get_mime_by_extension($file_info['server_path']);
			$a_files[] = array(
				$file_name, 
				$this->_get_filesize($file_info['size']), 
				unix_to_human($file_info['date']), 
				$mime_type, 
				$a_actions['edit']. "|". $a_actions['preview']
			);
		}
		
		// echo "<pre>";
		// print_r($files);
		// echo "</pre>";
		
		$this->table->set_heading(array('Ten file', 'Kich thuoc', 'Ngay cap nhat', 'MIME', 'Thao tac'));
		
		$this->data["head"]["title"] = "Import files list";
		$this->data["body"]["content"] = $this->table->generate($a_files); 
		$this->_show($this->data);
	}
	
	function preview() {
		$file_name = $this->uri->segment(3);
		
		$this->excel->set_file( $file_name );
		$this->excel->set_sheet( 'nhaphang_new' );
		$this->excel->set_template();
		$this->excel->get_sheet_range();
		$this->excel->display();
		// $this->excel->set_sheet_range('A:5', 'M:13');
		
		
	}
	
	function excel() {
		$this->excel->set_file();
		$this->excel->display();
	}
	
	function _show ($data) {
		$this->template->set_partial('header', 'partials/header', $data["head"]);
		$this->template->title($data["head"]["title"]);
		$this->template->build('body', $data["body"]);
	}
	
	function _get_filesize ($dsize) {
		if (strlen($dsize) <= 9 && strlen($dsize) >= 7) {
			$dsize = number_format($dsize / 1048576,1);
			return "$dsize MB";
		} elseif (strlen($dsize) >= 10) {
			$dsize = number_format($dsize / 1073741824,1);
			return "$dsize GB";
		} else {
			$dsize = number_format($dsize / 1024,1);
			return "$dsize KB";
		}
	}
	
	
	
}